﻿using System.Collections.Generic;
using System.Windows;
using System;

namespace Jerry.DroidBuilder.ThemeEditor.Controls
{
    public class DrawableInfo
    {
        public class AreaInfo
        {
            public int Row { get; set; }

            public int Column { get; set; }

            public string ImageSource { get; set; }

            public Rect Rect { get; set; }
        }

        /// <summary>
        /// Initializes a new instance of <see cref="DrawableInfo"/>.
        /// </summary>
        public DrawableInfo(string fileName, string drawableXml)
        {
            ImageSource = fileName;

            if (!string.IsNullOrEmpty(drawableXml))
            {
                IsComposited = true;
                DrawableXml = drawableXml;
            }

            if (fileName.EndsWith(".9.png", StringComparison.InvariantCultureIgnoreCase))
            {
                try
                {
                    Initialize(NinePatchDecoder.Decode(fileName, null));
                    IsPatched = true;
                }
                catch (Exception)
                {
                    // Empty.
                }
            }
        }

        public bool IsPatched { get; private set; }

        public bool IsComposited { get; private set; }

        public string DrawableXml { get; private set; }

        public string ImageSource { get; private set; }

        public double ImageWidth { get; private set; }

        public double ImageHeight { get; private set; }

        public IEnumerable<GridLength> Rows { get; private set; }

        public IEnumerable<GridLength> Columns { get; private set; }

        public Thickness Padding { get; private set; }

        public IEnumerable<AreaInfo> Areas { get; private set; }

        private void Initialize(NinePatchInfo patchInfo)
        {
            var areas = new List<AreaInfo>();
            var rows = new List<GridLength>();
            var columns = new List<GridLength>();

            var axisX = new List<int>(patchInfo.HorizontalStretchableAreas);
            var axisY = new List<int>(patchInfo.VerticalStretchableAreas);

            if (axisX[axisX.Count - 1] != patchInfo.ImageWidth)
            {
                axisX.Add(patchInfo.ImageWidth);
            }

            if (axisY[axisY.Count - 1] != patchInfo.ImageHeight)
            {
                axisY.Add(patchInfo.ImageHeight);
            }

            var lastX = 0;
            var lastY = 0;

            for (int i = 0; i < axisY.Count; i++)
            {
                var y = axisY[i];
                var height = y - lastY;

                lastX = 0;

                for (int k = 0; k < axisX.Count; k++)
                {
                    var x = axisX[k];
                    var width = x - lastX;

                    var area = new AreaInfo
                    {
                        Rect = new Rect(lastX, lastY, width, height),
                        Row = i,
                        Column = k,
                        ImageSource = ImageSource,
                    };

                    areas.Add(area);

                    if (columns.Count < axisX.Count)
                    {
                        columns.Add(new GridLength(width, k % 2 == 0 ? GridUnitType.Pixel : GridUnitType.Star));
                    }

                    lastX = x;
                }

                rows.Add(new GridLength(height, i % 2 == 0 ? GridUnitType.Pixel : GridUnitType.Star));

                lastY = y;
            }

            Areas = areas;
            Rows = rows;
            Columns = columns;
            ImageWidth = patchInfo.ImageWidth;
            ImageHeight = patchInfo.ImageHeight;
            Padding = new Thickness(patchInfo.PaddingLeft, patchInfo.PaddingTop, patchInfo.PaddingRight, patchInfo.PaddingBottom);
        }
    }
}
